package com.hairline.hairlinemarket.service.impl;

import com.hairline.hairlinemarket.bean.MarketOrder;
import com.hairline.hairlinemarket.bean.MarketOrderExample;
import com.hairline.hairlinemarket.bean.data.OrderIndex;
import com.hairline.hairlinemarket.mapper.MarketOrderGoodsMapper;
import com.hairline.hairlinemarket.mapper.MarketOrderMapper;
import com.hairline.hairlinemarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author:杨小凯
 * Date:2023-04-23 9:20
 * Description:<描述>
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MarketOrderMapper marketOrderMapper;

    @Autowired
    private MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Override
    public OrderIndex selectData(Integer userId) {

        // 待评价订单商品的数量
        Integer uncomment = marketOrderMapper.selectCountUncomment(userId);

        // 未支付订单:如果pay_id =null或者pay_id=''表示该订单未支付
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.createCriteria().andPayIdIsNull().andUserIdEqualTo(userId);

        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        Integer unpaid;
        if (marketOrders!=null&&marketOrders.size()>0){
            unpaid = marketOrders.size();
        }else {
            unpaid = 0;
        }

        // 待收货：当前时间在发货时间后
        MarketOrderExample marketOrderExample1 = new MarketOrderExample();
        marketOrderExample1.createCriteria().andShipTimeLessThan(new Date()).andUserIdEqualTo(userId);
        List<MarketOrder> marketOrders1 = marketOrderMapper.selectByExample(marketOrderExample1);
        Integer unrecv;
        if (marketOrders1!=null&&marketOrders1.size()>0){
            unrecv = marketOrders1.size();
        }else {
            unrecv = 0;
        }

        // 待发货：当前时间在发货时间前
        MarketOrderExample marketOrderExample2 = new MarketOrderExample();
        marketOrderExample2.createCriteria().andShipTimeGreaterThan(new Date()).andUserIdEqualTo(userId);
        List<MarketOrder> marketOrders2 = marketOrderMapper.selectByExample(marketOrderExample1);
        Integer unship;
        if (marketOrders2!=null&&marketOrders2.size()>0){
            unship = marketOrders2.size();
        }else {
            unship = 0;
        }
        OrderIndex orderIndex = new OrderIndex(uncomment, unpaid, unrecv, unship);
        return orderIndex;
    }
}
